﻿var zombies = zombies || {};
var windowURL = window.URL || window.webkitURL;

zombies.SubcribeList = function () {
    var self = this;

    self.Pages = ko.observableArray();
    self.Loaded = ko.observable(false);
    self.MemSubcribes = ko.observableArray();
    self.CurrentPage = ko.observable();
    self.Avatar = ko.observable("../Advertise/DefaultAssets/NoAvatar.gif");
    self.FullName = ko.observable();

    self.ChangePage = function (page) {
        self.MemSubcribes.removeAll();

        $.ajax({
            url: '/api/MemberProfile/GetSubcribeList/' + page,
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        }).done(function (request) {
            self.Pages.removeAll();
            for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
                self.Pages.push(i);
            }
            $.each(request.list, function (idx, item) {
                self.MemSubcribes.push(new zombies.MemSubcribe(item));
            });
        }).fail(function (request) {
            self.Loaded(false);
        });
    };

    self.RemoveSubcribe = function (item) {
        $.ajax({
            type: "POST",
            url: "/api/MemberProfile/RemoveSubcribe/" + item.SubId,
            contentType: "application/json",
            dataType: 'json',
            success: function (data) {
                self.MemSubcribes.remove(item);
                if (self.CurrentPage() > 1 && self.MemSubcribes().length == 0) {
                    var totalPage = self.Pages().length - 1;

                    self.Pages.removeAll();
                    for (i = 1; i <= totalPage && totalPage > 1; i++) {
                        self.Pages.push(i);
                    }

                    self.CurrentPage(self.CurrentPage() - 1);
                    load(self.CurrentPage());
                }
                toastr.success("Hủy theo dõi thành công!");
            },
            error: function (error) {
                toastr.error("Hủy theo dõi thất bại!");
            }
        });
    };
};

zombies.MemSubcribe = function (item) {
    var self = this;

    self.SubId = item.SubId;
    self.HtmlTag = "<img src='../" + item.LogoLink + "' style='width: 150px; height: 150px;'><h6>" + item.CompanyName + "</h6>";
    self.Link = '../../Home/Publisher/' + item.PubId;
};

var load = function (cur) {

    $.ajax({
        type: "GET",
        url: "/api/MemberProfile/GetUserProfile",
        contentType: "application/json",
        dataType: 'json'
    }).done(function (item) {
        if (item.Avatar != null && item.Avatar != '') {
            subcribeListViewModel.Avatar(item.Avatar);
        }
        subcribeListViewModel.FullName(item.FirstName + ' ' + item.LastName);
        subcribeListViewModel.Loaded(true);
    });

    if (subcribeListViewModel.Loaded) {
        subcribeListViewModel.CurrentPage(cur);
        subcribeListViewModel.MemSubcribes.removeAll();

        $.ajax({
            url: '/api/MemberProfile/GetSubcribeList/' + cur,
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        }).done(function (request) {
            for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
                subcribeListViewModel.Pages.push(i);
            }
            $.each(request.list, function (idx, item) {
                subcribeListViewModel.MemSubcribes.push(new zombies.MemSubcribe(item));
            });
        }).fail(function (request) {
            subcribeListViewModel.Loaded(false);
        });
    }
};

var subcribeListViewModel;
$(document).ready(function () {
    subcribeListViewModel = new zombies.SubcribeList();
    load(1);
    ko.applyBindings(subcribeListViewModel, document.getElementById("my-container"));
});